layui.define(['jquery', 'layer', 'laypage', 'laytpl'], function(exports){
    var $ = layui.jquery;
    var layer = layui.layer;
    var laypage = layui.laypage;
    var laytpl = layui.laytpl;
    var x = {};
    var code = '';
    
    function getRequest(url, successCallback, failCallback)
    {
        function success(response)
        {
            if(response.ok == true && response.status == 200){
                response.json().then(function(json){
                    successCallback(json);
                });
            }
        }
        
        function fail(response)
        {
            failCallback(response);
        }
        
        fetch(url).then(success).catch(fail);
    }
    
    
    
    function list(id, count, limit, jumpCallback, url)
    {
        laypage.render({
            elem: 'page' + id,
            count: count,
            curr: 1,
            limit: limit,
            theme: '#1E9FFF',
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function(obj, first){
                jumpCallback(obj.curr, obj.limit, id, url);
            }
        });
    }
    
    function doClick(id)
    {
        layer.open({
          type: 1,
          skin: 'layui-layer-rim', //加上边框
          area: ['960px', '540px'], //宽高
          content: `<div id="html"></div><div id="page${id}"></div>`
        });
        
        list(id, 300, 10, doPage, "");
    }
    
    function doPage(currentPage, limit)
    {
        
        
        getRequest(`http://127.0.0.1:30000/Api/FieldType/index?page=${currentPage}&limit=${limit}`, success, function(){});
    }
    
    function success(json)
    {
        laytpl(code).render(json, function(html){
         $("#html").html(html);
        });
        
    }
    
    x.set = function(id, xcode)
    {
        code = xcode;
        var theId = "#" + id;
        $(theId).click(function(){
            doClick(id);
        });

    };
    
    
    
    
    

    
    
    
    
    
    exports('picker', x);
});